//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/statline.h>
#include <wx/datetime.h>
#include "schedulerctrl.h"
#include "scheduleroptions.h"



enum cComboID
{
	cCBRebootEnabled=15555,
	cIDSchedulingCtrl

	/*
	cRadioNever=15555,
	cRadioDaily,
	cRadioWeekly,
	cRadioMonthly,
	cTXTMonthly,
	*/
};

BEGIN_EVENT_TABLE(CSchedulerOptions, wxPanel)
	EVT_UPDATE_UI(cIDSchedulingCtrl, CSchedulerOptions::OnSchedUpdateUI)
END_EVENT_TABLE()

CSchedulerOptions::CSchedulerOptions(wxWindow* pParent):wxPanel( pParent, wxID_ANY )
{
	int nFlags      = wxLEFT|wxUP|wxRIGHT|wxALIGN_CENTRE_VERTICAL;
	int nFlagsSave  = wxRIGHT|wxALIGN_CENTRE_VERTICAL;
	int nBorder		= 5;

	wxBoxSizer* pSizer = new wxBoxSizer(wxVERTICAL);

	m_arrSched.resize(7*24, false); 


	m_pcbRebootRouter = new wxCheckBox(this, cCBRebootEnabled, _("Enable scheduled router reboot"));
	pSizer->Add( m_pcbRebootRouter, 0, nFlags, nBorder);


	m_pctrScheduler = new CSchedulerCtrl(this, cIDSchedulingCtrl, m_arrSched);
	pSizer->Add( m_pctrScheduler, 0, nFlags, nBorder);

	m_pctrScheduler->SetEnableColor( wxColor(250,0,0) );

	/*
	wxStaticText* pTxt0 = new wxStaticText(this, wxID_ANY, _("Reboot Router:"));
	pSizer->Add( pTxt0, 0, nFlags, nBorder);

	m_prbNever = new wxRadioButton(this, cRadioNever, _("Never") );
	pSizer->Add( m_prbNever, 0, nFlags, nBorder );

	m_prbDaily = new wxRadioButton(this, cRadioDaily, _("Daily") );
	pSizer->Add( m_prbDaily, 0, nFlags, nBorder );

	m_prbWeekly = new wxRadioButton(this, cRadioWeekly, _("Weekly") );
	pSizer->Add( m_prbWeekly, 0, nFlags, nBorder );

	m_prbMonthly = new wxRadioButton(this, cRadioMonthly, _("Monthly") );
	pSizer->Add( m_prbMonthly, 0, nFlags, nBorder );

	wxBoxSizer* pTime = new wxBoxSizer(wxHORIZONTAL);
	wxStaticText* pTxt = new wxStaticText(this, wxID_ANY, _("Time:"));
	pTime->Add( pTxt, 0, wxLEFT|wxUP|wxALIGN_CENTRE_VERTICAL, nBorder);
	m_ptxtTime  = new wxTextCtrl(this, cTXTMonthly );
	pTime->Add( m_ptxtTime, 0, nFlags, nBorder);
	pSizer->Add( pTime, 0, nFlags, nBorder );

	//Line
	pSizer->Add(new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxSize(10,2), wxLI_HORIZONTAL), 0, wxALL|wxEXPAND, 10);


	m_pWeek = new wxFlexGridSizer(2, 1, 0, 0);

	//Weekly
	for (int i=wxDateTime::Sun;i=wxDateTime::Inv_WeekDay;i++)
	{
		wxString strLb =  wxDateTime::GetWeekDayName( (wxDateTime::WeekDay)i, wxDateTime::Name_Full);
		m_pWeeks[i] = new wxCheckBox(this, wxID_ANY, strLb );
		m_pWeek->Add( m_pWeeks[i], 0, nFlags, nBorder );
	}
	pSizer->Add( m_pWeek, 0, nFlags, nBorder );
	*/


	SetSizer(pSizer);
}

CSchedulerOptions::~CSchedulerOptions(void)
{
}

void CSchedulerOptions::OnSchedUpdateUI(wxUpdateUIEvent& event)
{
	if ( m_pcbRebootRouter )
	{
		event.Enable( m_pcbRebootRouter->GetValue() );
	}
}
//////////////////////////////////////////////////////////////////////////
std::string	 CSchedulerOptions::GetScheduler()
{
	MACENTRY entry;
	entry.arrSched = m_arrSched;

	return entry.SchedToString();
}

void CSchedulerOptions::SetScheduler(const std::string& str )
{
	MACENTRY entry(false);
	
	entry.SchedFromString( str );

	m_arrSched = entry.arrSched;

	m_pctrScheduler->Refresh();
}

bool CSchedulerOptions::GetSchedulerEnabled()
{
	return m_pcbRebootRouter->GetValue();
}

void CSchedulerOptions::SetSchedulerEnabled(bool bSet)
{
	return m_pcbRebootRouter->SetValue( bSet );
}

